package com.gwt.auth.service;

import com.gwt.auth.vo.AuthUser;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public interface AuthService {

    /**
     * 用户登录，生成token
     * @param userId 用户ID
     * @return AuthUser
     */
    AuthUser userLogin(Object userId);

    /**
     * 用户登录，生成token
     * @param userId 用户ID
     * @param userRoleIds 用户角色ID集合
     * @return AuthUser
     */
    AuthUser userLogin(Object userId, List<Object> userRoleIds);


    /**
     * 获取当前登录人ID
     * @return Object
     */
    <T> T getUserId(Class<T> clazz);

    /**
     * 校验是否登录，登录返回用户信息，未查询到抛出异常
     * @return AuthUser
     */
    AuthUser checkLogin();

    /**
     * 用户登出，清空token和缓存
     */
    void loginOut();

    /**
     * 刷新Token
     * @return
     */
    AuthUser tokenRefresh();

    /**
     * 刷新Token
     * @return
     */
    AuthUser tokenRefresh(AuthUser authUser);

    /**
     * 验证用户token
     * @return boolean
     */
    AuthUser tokenValidate();

    /**
     * 获取用户角色
     * @return List<Long>
     */
    List<Object> getUserRoles();


    /**
     * token验证是否过期(距上次操作时间是否超时)
     * @param authUser 缓存对象
     * @return boolean
     */
    boolean checkLoginExpTime(AuthUser authUser);
    /**
     * 获取Request
     * @return HttpServletRequest
     */
    default HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        assert requestAttributes != null;
        return (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
    }

    /**
     * 获取Response
     * @return HttpServletResponse
     */
    default HttpServletResponse getResponse() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        assert servletRequestAttributes != null;
        return servletRequestAttributes.getResponse();
    }
}
